package handler

/*
 +----------------------------------------------------------------------
 + Title        : DemoHandler
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2018-01-06
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 案例处理器
 +----------------------------------------------------------------------
*/
import (
	"context"
	"fmt"
	pbDemo "user-service/proto/demo"
	pbResult "user-service/proto/result"

	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
)

type DemoHandler struct {
	pbDemo.UnimplementedDemoSrvServer
}

/**
 * 转出
 *
 * @param int64 Uid -------------------------------------------非必传
 * @param int64 Amount ----------------------------------------非必传
 * @param string TransOutResult -------------------------------非必传
 * @param string TransInResult --------------------------------非必传
 * @return *emptypb.Empty, error
 * @author huwl
 */
func (e *DemoHandler) TransOut(ctx context.Context, req *pbDemo.ReqGrpc) (*emptypb.Empty, error) {
	fmt.Println("----------------- 转出 -----------------")
	if req.TransOutResult == "FAILURE" {
		return &emptypb.Empty{}, status.New(codes.Aborted, "FAILURE 转入失败").Err()
	} else if req.TransOutResult == "ONGOING" {
		return &emptypb.Empty{}, status.New(codes.FailedPrecondition, "ONGOING 转入失败").Err()
	}
	return &emptypb.Empty{}, nil
}

/**
 * 补偿转出
 *
 * @param int64 Uid -------------------------------------------非必传
 * @param int64 Amount ----------------------------------------非必传
 * @param string TransOutResult -------------------------------非必传
 * @param string TransInResult --------------------------------非必传
 * @return *emptypb.Empty, error
 * @author huwl
 */
func (e *DemoHandler) TransOutRevert(ctx context.Context, in *pbDemo.ReqGrpc) (*emptypb.Empty, error) {
	fmt.Println("----------------- 补偿转出 -----------------")
	return &emptypb.Empty{}, nil
}

/**
 * 确认转出
 *
 * @param int64 Amount ----------------------------------------非必传
 * @param string TransOutResult -------------------------------非必传
 * @param string TransInResult --------------------------------非必传
 * @return *emptypb.Empty, error
 * @author huwl
 */
func (e *DemoHandler) TransOutConfirm(ctx context.Context, in *pbDemo.ReqGrpc) (*emptypb.Empty, error) {
	fmt.Println("----------------- 确认转出 -----------------")
	return &emptypb.Empty{}, nil
}

/**
 * 取消转出
 *
 * @param int64 Amount ----------------------------------------非必传
 * @param string TransOutResult -------------------------------非必传
 * @param string TransInResult --------------------------------非必传
 * @return *emptypb.Empty, error
 * @author huwl
 */
func (e *DemoHandler) TransOutCancel(ctx context.Context, in *pbDemo.ReqGrpc) (*emptypb.Empty, error) {
	fmt.Println("----------------- 取消转出 -----------------")
	return &emptypb.Empty{}, nil
}

/**
 * 转入
 *
 * @param int64 Uid -------------------------------------------非必传
 * @param int64 Amount ----------------------------------------非必传
 * @param string TransOutResult -------------------------------非必传
 * @param string TransInResult --------------------------------非必传
 * @return *pbResult.Result, error
 * @author huwl
 */
func (e *DemoHandler) TransIn(ctx context.Context, req *pbDemo.ReqGrpc) (*pbResult.Result, error) {
	fmt.Println("----------------- 转入 -----------------")
	if req.TransInResult == "FAILURE" {
		return &pbResult.Result{Code: 400, Msg: "FAILURE"}, status.New(codes.Aborted, "FAILURE 转入失败").Err()
	} else if req.TransInResult == "ONGOING" {
		return &pbResult.Result{Code: 400, Msg: "ONGOING"}, status.New(codes.FailedPrecondition, "ONGOING 转入失败").Err()
	}
	return &pbResult.Result{
		Code: 200,
		Msg:  "转入接口",
		Data: map[string]string{
			"id":   "100",
			"name": "xxx",
		},
	}, nil
}

/**
 * 补偿转入
 *
 * @param int64 Uid -------------------------------------------非必传
 * @param int64 Amount ----------------------------------------非必传
 * @param string TransOutResult -------------------------------非必传
 * @param string TransInResult --------------------------------非必传
 * @return *pbResult.Result error
 * @author huwl
 */
func (e *DemoHandler) TransInRevert(ctx context.Context, in *pbDemo.ReqGrpc) (*pbResult.Result, error) {
	fmt.Println("----------------- 补偿转入 -----------------")
	return &pbResult.Result{
		Code: 200,
		Msg:  "补偿转入",
		Data: map[string]string{
			"id":   "222",
			"name": "zzz",
		},
	}, nil
}

/**
 * 确认转入
 *
 * @param int64 Uid -------------------------------------------非必传
 * @param int64 Amount ----------------------------------------非必传
 * @param string TransOutResult -------------------------------非必传
 * @param string TransInResult --------------------------------非必传
 * @return *emptypb.Empty, error
 * @author huwl
 */
func (e *DemoHandler) TransInConfirm(ctx context.Context, in *pbDemo.ReqGrpc) (*emptypb.Empty, error) {
	fmt.Println("----------------- 确认转入 -----------------")
	return &emptypb.Empty{}, nil
}

/**
 * 取消转入
 *
 * @param int64 Uid -------------------------------------------非必传
 * @param int64 Amount ----------------------------------------非必传
 * @param string TransOutResult -------------------------------非必传
 * @param string TransInResult --------------------------------非必传
 * @return *pbDemo.Result, error
 * @author huwl
 */
func (e *DemoHandler) TransInCancel(ctx context.Context, in *pbDemo.ReqGrpc) (*emptypb.Empty, error) {
	fmt.Println("----------------- 取消转入 -----------------")
	return &emptypb.Empty{}, nil
}

/**
 * XA 转入
 *
 * @param int64 Uid -------------------------------------------非必传
 * @param int64 Amount ----------------------------------------非必传
 * @param string TransOutResult -------------------------------非必传
 * @param string TransInResult --------------------------------非必传
 * @return *pbDemo.Result, error
 * @author huwl
 */
func (e *DemoHandler) TransInXa(ctx context.Context, in *pbDemo.ReqGrpc) (*emptypb.Empty, error) {
	fmt.Println("----------------- XA 转入 -----------------")
	return &emptypb.Empty{}, nil
}

/**
 * XA 转出
 *
 * @param int64 Uid -------------------------------------------非必传
 * @param int64 Amount ----------------------------------------非必传
 * @param string TransOutResult -------------------------------非必传
 * @param string TransInResult --------------------------------非必传
 * @return *pbDemo.Result, error
 * @author huwl
 */
func (e *DemoHandler) TransOutXa(ctx context.Context, in *pbDemo.ReqGrpc) (*emptypb.Empty, error) {
	fmt.Println("----------------- XA 转出 -----------------")
	return &emptypb.Empty{}, nil
}
